/*
 * AniVisApp.java
 */

package anivis;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.Properties;

import anivis.ui.AniVisFrame;
import anivis.ui.Controller;
import anivis.db.QueryLayer;
import anivis.ui.map.MapView;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

import com.bbn.openmap.MultipleSoloMapComponentException;

/**
 * The main class of the application.
 */
public class AniVisApp extends Application {

    private Controller controller;
    private Properties properties;
    
    public static AniVisApp application;
    
    public AniVisApp() {
        
    }
    
    /**
     * Main method launching the application.
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException, MultipleSoloMapComponentException {
        // properties of the application
        Properties props = new Properties();
        loadResource("AniVis.properties", props);
        // create the app
        application = new AniVisApp();
        // create the controller
        Controller controller = new Controller();
        
        // prepare the stuff before creating the frame
        application.controller = controller;
        application.properties = props;
        
        // ui frame
        AniVisFrame anivisFrame = new AniVisFrame(props, controller);

        // and display the frame
        anivisFrame.pack();
        anivisFrame.setVisible(true);
    }
    
    public String getString(String property) {
        return properties.getProperty(property);
    }
       
    public static AniVisApp getApplication() {
        return application;
    }
    
    public Controller getController() {
        return controller;
    }
    
    /**
     * This method, called from main(), bundles functionality that
     * once was being called twice, because there were two resource
     * files being loaded, not just one, as is currently the case.
     * Rather than put this code back into main(), it's been kept as a
     * separate method in case we use more than one resource file
     * again.
     */
    private static void loadResource(String resources, Properties props) {
        try {
            InputStream in = new FileInputStream(resources);
            if (props == null) {
                System.err.println("Unable to locate resources: " + resources);
                System.err.println("Using default resources.");
            } else {
                try {
                    props.load(in);
                } catch (java.io.IOException e) {
                    System.err.println("Caught IOException loading resources: "
                                    + resources);
                    System.err.println("Using default resources.");
                }
            }
        }
        catch (Exception e) {
            System.err.println(e);
        }
    }

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        
    }
}
